﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SSCAI.DAL;
using SSCAI.Common;

namespace SSCAI.Model
{
    public class SnModel
    {
        public int Id { get; set; }
        public string ExpiredDate { get; set; }
        public string UserName { get; set; }
        public string Sn { get; set; }
    }

    public class SnModels
    {
        public List<SnModel> Sns { get; set; }

        private DateTime GetVaildTime(string sn)
        {
            sn = sn.Replace("-", string.Empty);
            return CalculateHelper.GetExpriedDate(sn);
        }

        public SnModels()
        {
            int index = 0;

            this.Sns = XmlHelper.GetUserInfoItems().Select(u => new SnModel
            {
                Id = index++,
                ExpiredDate = this.GetVaildTime(u.Attribute("sn").Value.ToString()).ToShortDateString(),
                Sn = u.Attribute("sn").Value.ToString(),
                UserName = CalculateHelper.GetUserName(u.Attribute("sn").Value.ToString())
            }).ToList();
        }

        public void UpdateUserInfo(string sn)
        {
           
            XmlHelper.UpdateUserInfo(sn);
            SSCAI.Common.CalculateHelper.ExpiredDate = CalculateHelper.GetExpriedDate(sn);
            SSCAI.Common.CalculateHelper.UserName = CalculateHelper.GetUserName(sn);
        }
    }
}
